//---------------------------------------------------------------------------

#include "http_stringlist.h"
#include <windows.h>

//---------------------------------------------------------------------------

string_data::string_data() {
        next = NULL;
}

string_data::~string_data() {
        if (next) {
                delete next;
        }
}

http_stringlist::http_stringlist() {
        str_data = new string_data;
        strings_count = 0;
}

http_stringlist::http_stringlist(http_stringlist &rhs) {
        str_data = new string_data;
        strings_count = 0;
        int count = rhs.GetCount();
        if (count > 0) {
                std::string name;
                std::string value;
                for (int i = 0; i < count; i++) {
                        name = rhs.GetName(i);
                        value = rhs.GetValue(i);
                        this->Add(name,value);
                }
        }
}

http_stringlist::~http_stringlist() {
        if (str_data) {
                delete str_data;
        }
}

std::string http_stringlist::GetName(int index) {
        std::string result;
        if (index < strings_count || index >= 0) {
                int i = 0;
                string_data *pData = str_data;
                while(1) {
                        if (i == index) {
                                result = pData->name;
                                break;
                        }
                        pData = pData->next;
                        if (pData == NULL) {
                                break;
                        }
                        i++;
                }
        }
        return result;
}

std::string http_stringlist::GetValue(int index) {
        std::string result;
        if (index < strings_count || index >= 0) {
                int i = 0;
                string_data *pData = str_data;
                while(1) {
                        if (i == index) {
                                result = pData->value;
                                break;
                        }
                        pData = pData->next;
                        if (pData == NULL) {
                                break;
                        }
                        i++;
                }
        }
        return result;
}

bool http_stringlist::Exist(std::string name) {
        bool result = false;
        if (name.length() > 0) {
                string_data *pData = FindString(name);
                if (pData) {
                        result = true;
                }
        }
        return result;
}

void http_stringlist::Add(std::string name, std::string value) {
        if (name.length() > 0 && value.length() > 0) {
                string_data *pData = GetString();
                pData->name = name;
                pData->value = value;
        }
}

bool http_stringlist::Update(std::string name, std::string value) {
        bool result = false;
        if (name.length() > 0 && value.length() > 0) {
                string_data *pData = FindString(name);
                if (pData) {
                        pData->value = value;
                        result = true;
                }
        }
        return result;
}

void http_stringlist::Clear() {
        if ( str_data ) {
                delete str_data;
                str_data = new string_data;
                strings_count = 0;
        }
}

//----- Private Function --------

string_data *http_stringlist::GetString() {
        string_data *result = str_data;
        if (strings_count > 0) {
                while(1) {
                        if (result->next) {
                                result = result->next;
                        }
                        else {
                                result->next = new string_data;
                                result = result->next;
                                break;
                        }
                }
        }
        strings_count++;
        return result;
}

string_data *http_stringlist::FindString(std::string name) {
        string_data *result = NULL;
        string_data *temp = str_data;
        if (strings_count == 0) {
                return result;
        }
        for (int i = 0; i < strings_count; i++) {
                if (temp->name.compare(name) == 0) {
                        result = temp;
                        break;
                }
                temp = temp->next;
        }
        return result;
}
